package com.aft.terra.modules.game.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.aft.terra.app.Constants;
import com.aft.terra.common.bean.DateBean;
import com.aft.terra.common.bean.Message;
import com.aft.terra.common.utils.CcbMd5Util;
import com.aft.terra.common.utils.DateUtil;
import com.aft.terra.common.utils.RedisKit;
import com.aft.terra.modules.basic.model.Adver;
import com.aft.terra.modules.game.model.GameDetails;
import com.aft.terra.modules.game.model.Intercept;
import com.aft.terra.modules.game.model.Lucky;
import com.aft.terra.modules.game.model.MonthlyList;
import com.aft.terra.modules.game.model.OnList;
import com.aft.terra.modules.game.model.Orders;
import com.aft.terra.modules.game.model.RankingList;
import com.aft.terra.modules.game.model.Request;
import com.aft.terra.modules.game.model.Rule;
import com.aft.terra.modules.game.model.Share;
import com.aft.terra.modules.game.model.ShareUser;
import com.aft.terra.modules.game.model.User;
import com.aft.terra.modules.game.model.WeeklyChart;
import com.aft.terra.modules.game.model.Winning;
import com.aft.terra.modules.game.service.GameService;
import com.aft.terra.modules.member.model.Wechat;
import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.redis.Cache;

public class GameServiceImpl implements GameService {

	/**
	 * 获取当前用户剩余多少次游戏机会 
	 * userId 用户的id
	 */
	@Override
	public User querUser(String userId) {
		Calendar cDay = Calendar.getInstance();
		cDay.add(Calendar.DAY_OF_MONTH, -1);
		return User.dao.findById(userId)
				.put("orders",Orders.dao.queryCount(Long.valueOf(userId),DateUtil.formatDate(cDay.getTime())))
				.put("date_orders",Orders.dao.queryCount(Long.valueOf(userId),DateUtil.now()))
				.put("share",Share.dao.queryCount(Long.valueOf(userId),DateUtil.now()))
				.put("socreSum",getRankingListScoreSum(userId))
				.put("winning_count",Winning.dao.queryUserId(Long.valueOf(userId),DateUtil.formatDate(new Date())));
	}
	
	/**
	 * 获取总榜数据
	 * 
	 * @return 数据集合
	 */
	@Override
	public List<RankingList> queryRankingList() {
		return RankingList.dao.findAll();
	}

	/**
	 * 获取日榜排行榜数据
	 * 
	 * @return 数据集合
	 */
	@Override
	public List<OnList> queryOnList(String date) {
		if(StringUtils.isBlank(date) || "undefined".equals(date)){
			date = new StringBuffer().append(DateUtil.formatDate(new Date()))
					.append("~").append(DateUtil.formatDate(new Date())).toString();
		}
		return OnList.dao.findAll(date);
	}

	/**
	 * 获取周榜排行榜数据
	 * 
	 * @return 数据集合
	 */
	@Override
	public List<WeeklyChart> queryWeeklyChart(String date) {
		if(StringUtils.isBlank(date) || "undefined".equals(date)){
			date = new StringBuffer().append(DateUtil.fromatStringToDayOfWeek())
					.append("~").append(DateUtil.fromatStringToFirstDayOfWeek()).toString();
		}
		return WeeklyChart.dao.findAll(date);
	}

	/**
	 * 获取月榜排行榜数据
	 * 
	 * @return 数据集合
	 */
	@Override
	public List<MonthlyList> queryMonthlyList(String date) {
		if(StringUtils.isBlank(date) || "undefined".equals(date)){
			date = new StringBuffer().append(DateUtil.fromatStringToDayOfMonth())
					.append("~").append(DateUtil.fromatStringToFirstDayOfMonth()).toString();
		}
		return MonthlyList.dao.findAll(date);
	}

	/**
	 * 开始游戏
	 * @return 对象 前台需要用他进行判断处理
	 */
	@Override
	public Message startGame(Controller controler,String userId) {
		Message message = new Message();
		message.setSuccess(false);
		/**
		 * 获取用户剩余的游戏次数
		 */
//		User user = this.querUser(userId);
//		if(user.getPlayGameFrequency() > 0){
			
		    Enumeration headerNames = controler.getRequest().getHeaderNames();
		    Record reqeust = new Record();
		    reqeust.set("create_date", new Date());
		    while (headerNames.hasMoreElements()) {
		        String key = (String) headerNames.nextElement();
		        String value = controler.getRequest().getHeader(key);
		        reqeust.set(key, value);
		    }
//			Integer userCount = GameDetails.dao.queryCount(userId);
//			if(userCount.equals(0)){
//				ShareUser su = ShareUser.dao.findByUserId(userId);
//				if(su!=null&&ShareUser.dao.findByShareUserId(su.getShareUserId())){
//					su.setModifyDate(new Date());
//					su.setType("1");
//					su.update();
//					User.dao.updateUserGameCount(su.getShareUserId().toString(),1);
//				}
//			}
//			Boolean boo = User.dao.updateSubUserGameCount(userId,1);
//			if(boo){
//				GameDetails gd = new GameDetails();
//				gd.setCreateDate(new Date());
//				gd.setUserHeadimgurl(Wechat.dao.findById(user.getWechatId()).getWHeadimgurl());
//				gd.setUserId(user.getId());
//				gd.setUserName(user.getUserName());
//				/**
//				 * 查询用户的总积分
//				 */
//				message.setCurrentIntegral(0);
//				message.setScoreSum(getRankingListScoreSum(userId));
//				message.setGameCount(User.dao.queryUser(userId).getPlayGameFrequency());
//				message.setSuccess(gd.save());
//				message.setGdId(gd.getId());
//				reqeust.set("gd_id", gd.getId());
//			}
			reqeust.set("user_id", "11");
			Db.save("t_request", reqeust);
//		}
		return message;
	}
	
	/**
	 * 获取游戏是否开启
	 */
	@Override
	public Integer queryGameStartDateOnEndDate() {
		Cache cache = RedisKit.getCache();
		Map<String,String> map = cache.get(Constants.GAME_CONFIG_PARAMS);
		Integer gameStartDateOnEndDate = 0;
		DateUtil.formatStringToDate(map.get("game_start_time").toString());
		DateUtil.formatStringToDate(map.get("game_end_time").toString());
		if("1".equals(map.get("is_open_clear"))){
			String startTime = (String) map.get("clear_date_s");
			String endTime = map.get("clear_date_e");
			Long now = DateUtil.format(DateUtil.nowHHmmss(), "HH:mm:ss").getTime();
			Long st = DateUtil.format(startTime, "HH:mm:ss").getTime();
			Long nt = DateUtil.format(endTime, "HH:mm:ss").getTime();
			// 目前正在清理数据期间
			if(now > 0){
				if(now >= st){
					return 3;
				}
			}else{
				if(now <= nt){
					return 4;
				}
			}
		}
		
		if(new Date().getTime() < DateUtil.formatStringToDate(map.get("game_start_time").toString()).getTime()){
			return  1;
		}
		if(new Date().getTime() > DateUtil.formatStringToDate(map.get("game_end_time").toString()).getTime()){
			return  2;
		}
		
		return gameStartDateOnEndDate;
	}
	
	/**
	 * 更新游戏数据
	 */
	@Override
	public Message updateGameData(String gdId, String userId,Integer score) {
		Message message = new Message();
		message.setSuccess(false);
		/**
		 * 获取游戏数据
		 */
		GameDetails gd = GameDetails.dao.findById(gdId);
		gd.setIsEnd(true);
		gd.setModifyDate(new Date());
		gd.setScore(score);
		gd.update();
		saveOnList(userId);
		saveWeeklyChart(userId);
		saveMonthlyList(userId);
		saveRankingList(userId);
		message.setScoreSum(getRankingListScoreSum(userId));
		return message;
	}
	
	/**
	 * 获取根据用户获取游戏今日得分
	 * @param userId
	 * @return
	 */
	private Integer getRankingListScoreSum(String userId){
		GameDetails rl = GameDetails.dao.queryDateScore(DateUtil.formatDate(new Date()),DateUtil.formatDate(new Date()),Long.valueOf(userId));
		return rl!=null ? rl.getBigDecimal("scoreSum").intValue() : 0;
	}
	
	/**
	 * 保存  总榜
	 * @param userId
	 */
	public void saveRankingList(String userId){
		GameDetails userGameDetails = GameDetails.dao.queryDateScore(null,
				null,Long.valueOf(userId));
		/**
		 * 处理 日榜 周榜 月榜 总榜数据
		 */
		RankingList onList = RankingList.dao.findByUserId(userId,"2017-07-10"+"~"+"2017-09-30");
		User user = User.dao.queryUser(userId);

		if(onList!=null){
			RankingList.dao.updateIdOnScore(onList.getId(),userGameDetails.getBigDecimal("scoreSum").longValue(),User.dao.queryUser(userId));
		}else{
			if(userGameDetails.getBigDecimal("scoreSum").intValue() > 0){
				RankingList ol = new RankingList();
				ol.setCreateDate(new Date());
				ol.setScoreSum(userGameDetails.getBigDecimal("scoreSum").intValue());
				ol.setTimeQuantum("2017-07-10"+"~"+"2017-09-30");
				ol.setUserId(user.getId());
				ol.setModifyDate(new Date());
				ol.setUserName(user.getUserName());
				ol.setHeadimgurl(user.getStr("w_headimgurl"));
				ol.save();
			}
		}
	}
	
	/**
	 * 保存  月榜
	 * @param userId
	 */
	public void saveMonthlyList(String userId){
		String startDateOnEndDate = new StringBuffer()
				.append(DateUtil.fromatStringToDayOfMonth()).append("~")
				.append(DateUtil.fromatStringToFirstDayOfMonth()).toString();
		GameDetails userGameDetails = GameDetails.dao.queryDateScore(DateUtil.fromatStringToDayOfMonth(),
				DateUtil.fromatStringToFirstDayOfMonth(),Long.valueOf(userId));
		/**
		 * 处理 日榜 周榜 月榜 总榜数据
		 */
		MonthlyList onList = MonthlyList.dao.findByUserId(userId,startDateOnEndDate);
		User user = User.dao.queryUser(userId);

		if(onList!=null){
			MonthlyList.dao.updateIdOnScore(onList.getId(),userGameDetails.getBigDecimal("scoreSum").longValue(),User.dao.queryUser(userId));
		}else{
			if(userGameDetails.getBigDecimal("scoreSum").intValue() > 0){
				MonthlyList ol = new MonthlyList();
				ol.setCreateDate(new Date());
				ol.setModifyDate(new Date());
				ol.setScoreSum(userGameDetails.getBigDecimal("scoreSum").intValue());
				ol.setTimeQuantum(startDateOnEndDate);
				ol.setUserId(user.getId());
				ol.setUserName(user.getUserName());
				ol.setHeadimgurl(user.getStr("w_headimgurl"));
				ol.save();
			}
		}
	}
	
	/**
	 * 保存  周榜
	 * @param userId
	 */
	public void saveWeeklyChart(String userId){
		String startDateOnEndDate = new StringBuffer()
				.append(DateUtil.fromatStringToDayOfWeek()).append("~")
				.append(DateUtil.fromatStringToFirstDayOfWeek()).toString();
		GameDetails userGameDetails = GameDetails.dao.queryDateScore(DateUtil.fromatStringToDayOfWeek(),
				DateUtil.fromatStringToFirstDayOfWeek(),Long.valueOf(userId));
		/**
		 * 处理 日榜 周榜 月榜 总榜数据
		 */
		WeeklyChart onList = WeeklyChart.dao.findByUserId(userId,startDateOnEndDate);
		User user = User.dao.queryUser(userId);

			if(onList!=null){
				WeeklyChart.dao.updateIdOnScore(onList.getId(),userGameDetails.getBigDecimal("scoreSum").longValue(),User.dao.queryUser(userId));
			}else{
				if(userGameDetails.getBigDecimal("scoreSum").intValue() > 0){
					WeeklyChart ol = new WeeklyChart();
					ol.setCreateDate(new Date());
					ol.setModifyDate(new Date());
					ol.setScoreSum(userGameDetails.getBigDecimal("scoreSum").intValue());
					ol.setTimeQuantum(startDateOnEndDate);
					ol.setUserId(user.getId());
					ol.setUserName(user.getUserName());
					ol.setHeadimgurl(user.getStr("w_headimgurl"));
					ol.save();
				}
			}
	}
	
	
	/**
	 * 保存  日榜
	 * @param userId
	 */
	public void saveOnList(String userId){
		String date = DateUtil.now();
		String startDateOnEndDate = new StringBuffer()
				.append(date).append("~")
				.append(date).toString();
		GameDetails userGameDetails = GameDetails.dao.queryDateScore(date,
				date,Long.valueOf(userId));
		/**
		 * 处理 日榜 周榜 月榜 总榜数据
		 */
		OnList onList = OnList.dao.findByUserId(userId,startDateOnEndDate);
		User user = User.dao.queryUser(userId);
		if(onList!=null){
			 OnList.dao.updateIdOnScore(onList.getId(),userGameDetails.getBigDecimal("scoreSum").longValue(),User.dao.queryUser(userId));
		}else{
			if(userGameDetails.getBigDecimal("scoreSum").intValue() > 0){
				OnList ol = new OnList();
				ol.setCreateDate(new Date());
				ol.setModifyDate(new Date());
				ol.setScoreSum(userGameDetails.getBigDecimal("scoreSum").intValue());
				ol.setTimeQuantum(startDateOnEndDate);
				ol.setUserId(user.getId());
				ol.setUserName(user.getUserName());
				ol.setHeadimgurl(user.getStr("w_headimgurl"));
				ol.save();
			}
		}
	}
	
	/**
	 * 保存手机号
	 * @return
	 */
	@Override
	public Message updateUserPhone(String phone , String userId,User user) {
		Message message = new Message();
		message.setSuccess(false);
		if (StrKit.isBlank(user.getPhone()) && StrKit.notBlank(phone) && !User.dao.findByPhone(phone)) {
			user.setPhone(phone);
			user.update();
		} else {
			message.setSuccess(true);
			message.setMessage("该手机号已经绑定游戏");
		}
		return message;
	}
	
	/**
	 * 随机抽奖
	 * @param userId 用户id
	 * @return 数据对象
	 */
	@Override
	public Message randomLuckDraw(String userId) {
		Message message = new Message();
		message.setSuccess(false);
		double befor = 0.0;
		double end = 0.0;
		double randomNumber;
		message.setMessage("4");
		Winning w = new Winning();
		w.setCreateDate(new Date());
		w.setUserId(Long.valueOf(userId));
		Calendar cDay = Calendar.getInstance();
		cDay.add(Calendar.DAY_OF_MONTH, -1);
		if(Orders.dao.queryCount(Long.valueOf(userId),DateUtil.formatDate(cDay.getTime())).equals(30)
				&& Winning.dao.queryUserId(Long.valueOf(userId),DateUtil.formatDate(new Date())) <=0){
			List<Lucky> list = Lucky.dao.findByLucky();
			for (int i = 0; i < list.size(); i++) {
				randomNumber = Math.random();
				end += list.get(i).getLuckyRate().doubleValue();
				if (i > 0) {
					befor += list.get(i - 1).getLuckyRate().doubleValue();
				}
				if (befor < randomNumber && randomNumber <= end) {
					Integer stock = list.get(i).getLuckySum();
					if (stock > 0) {
						Lucky l = list.get(i);
						Long luckyId = l.getId();
						Lucky.dao.updateIdOnStack(luckyId);
						/**
						 * 1 类型为 游戏次数
						 */
						if(l.getLuckyType().equals("1")){
							User.dao.updateUserGameCount(userId,1);
						}else if(l.getLuckyType().equals("2")){
							User.dao.updateUserGameCount(userId,2);
						}
						message.setMessage(l.getLuckyType()+"");
						w.setLuckyId(luckyId);
						w.setLuckyName(l.getLuckyName());
						break;
					}
				}
			}
		}
		message.setSuccess(w.save());
		return message;
	}
	
	/**
	 * 分享 
	 *  @param userId 用户id
	 * @return 数据对象
	 */
	@Override
	public Message saveShareUserCount(String userId,String type) {
		Message message = new Message();
		message.setSuccess(false);
		/**
		 * 获取每天的分享次数
		 */
		Integer shareCount =  Share.dao.queryCount(Long.valueOf(userId), DateUtil.formatDate(new Date()));
		if(shareCount >= 3){
			message.setGameCount(User.dao.findById(userId).getPlayGameFrequency());
			return message;
		}
		// 更新用户的次数
		User.dao.updateUserGameCount(userId, 1);
		Share share = new Share();
		share.setCreateDate(new Date());
		share.setShareType(type);
		share.setShareTypeValue("1".equals(type) ? "朋友圈" : "朋友");
		share.setUserId(Long.valueOf(userId));
		share.save();
		message.setSuccess(true);
		message.setGameCount(User.dao.findById(userId).getPlayGameFrequency());
		return message;
	}
	
	
	/**
	 * 1分钱支付
	 * @author Administrator
	 */
	public String getCcbPay(Controller action) {
		String payment = action.getPara("payment");
		String url = "";
		String userId = action.getCookie(Constants.SESSION_NAME);
//		if (StringUtils.isNotBlank(userId)) {
//			User user = User.dao.findById(userId);
//			if (user != null) {
//				Integer i = Orders.dao.findByUserId(user.getId());
//				if (i < 30) {
					StringBuffer ccbStr = new StringBuffer();
//					BigDecimal prize = new BigDecimal(0.01);
//					Orders o = new Orders();
					String orderCode = DateUtil.getTimestamp() + DateUtil.nextval("order_no");
//					o.setCreateDate(new Date());
//					o.setOrdersCode(orderCode);
//					o.setPayMoney(prize);
//					o.setPhone(user.getPhone());
//					o.setUserName(user.getUserName());
//					o.setUserId(user.getId());
//					o.save();
					StringBuffer bank = new StringBuffer();
//					LogKit.info(orderCode +" -----------------------------------------orderCode");
//					LogKit.info(userId  +" -----------------------------------------userId");
					ccbStr.append("MERCHANTID=105220173992017&POSID=545089039&BRANCHID=220000000")
							.append("&ORDERID=" + orderCode + "&PAYMENT=").append(payment)
							.append("&CURCODE=01&TXCODE=520100").append("&REMARK1=http://fknz.changcl.com/pay/usedCallBack&REMARK2=" + userId
									+ "&TYPE=1&PUB=" + Constants.CCB_PAY_PUB + "&GATEWAY=&CLIENTIP=&REGINFO=")
							.append("&PROINFO=&REFERER=");
					// 需要返回的串
					bank.append("MERCHANTID=105220173992017&POSID=545089039&BRANCHID=220000000")
							.append("&ORDERID=" + orderCode + "&PAYMENT=").append(payment).append("&CURCODE=01&TXCODE=520100")
							.append("&REMARK1=http://fknz.changcl.com/pay/usedCallBack&REMARK2=" + userId + "&TYPE=1&GATEWAY=&CLIENTIP=&REGINFO=")
							.append("&PROINFO=&REFERER=");
					String mac = new CcbMd5Util().getMD5(ccbStr.toString().trim()).toLowerCase();
					String pub = bank.append("&MAC=" + mac).toString();
					url = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?" + pub;
					LogKit.info(url  +"                                     -----------------------------------------url");
//				} else {
//					url = "/";
//				}
//			}else{
//				url = "/";
//			}
//		}
		return url;

	}
	
	/**
	 * 5毛钱支付
	 * @author Administrator
	 */
	public String getNewCcbPay(Controller action) {
		String url = "";
		String userId = action.getCookie(Constants.SESSION_NAME);
		if (StringUtils.isNotBlank(userId)) {
			User user = User.dao.findById(userId);
			if (user != null) {
				Integer i = Orders.dao.findByUserId(user.getId());
				if (i <= 25) {
					StringBuffer ccbStr = new StringBuffer();
					BigDecimal prize = new BigDecimal(0.50);
					Orders o = new Orders();
					String orderCode = DateUtil.getTimestamp() + DateUtil.nextval("order_no");
					o.setCreateDate(new Date());
					o.setOrdersCode(orderCode);
					o.setPayMoney(prize);
					o.setPhone(user.getPhone());
					o.setUserName(user.getUserName());
					o.setUserId(user.getId());
					o.save();
					StringBuffer bank = new StringBuffer();
					ccbStr.append("MERCHANTID=105220173992017&POSID=545089039&BRANCHID=220000000")
							.append("&ORDERID=" + orderCode + "&PAYMENT=0.50")
							.append("&CURCODE=01&TXCODE=520100").append("&REMARK1=http://fknz.changcl.com/pay/newCallBack&REMARK2=" + userId
									+ "&TYPE=1&PUB=" + Constants.CCB_PAY_PUB + "&GATEWAY=&CLIENTIP=&REGINFO=")
							.append("&PROINFO=&REFERER=");
					// 需要返回的串
					bank.append("MERCHANTID=105220173992017&POSID=545089039&BRANCHID=220000000")
							.append("&ORDERID=" + orderCode + "&PAYMENT=0.50").append("&CURCODE=01&TXCODE=520100")
							.append("&REMARK1=http://fknz.changcl.com/pay/newCallBack&REMARK2=" + userId + "&TYPE=1&GATEWAY=&CLIENTIP=&REGINFO=")
							.append("&PROINFO=&REFERER=");
					String mac = new CcbMd5Util().getMD5(ccbStr.toString().trim()).toLowerCase();
					String pub = bank.append("&MAC=" + mac).toString();
					url = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?" + pub;
				} else {
					url = "/";
				}
			}else{
				url = "/";
			}
		}
		return url;
	}
	
	public String getNewCallBackPay(Controller action) {
		String url = "";
		String callBackUrl = action.getPara("callBackUrl");
		String orderCode = action.getPara("orderCode");
		String userId = action.getPara("userId");
		String money = action.getPara("money");
		StringBuffer ccbStr = new StringBuffer();
		StringBuffer bank = new StringBuffer();
		ccbStr.append("MERCHANTID=105220173992017&POSID=545089039&BRANCHID=220000000")
				.append("&ORDERID=" + orderCode + "&PAYMENT=" + money)
				.append("&CURCODE=01&TXCODE=520100").append("&REMARK1=" + callBackUrl + "&REMARK2=" + userId
						+ "&TYPE=1&PUB=" + Constants.CCB_PAY_PUB + "&GATEWAY=&CLIENTIP=&REGINFO=")
				.append("&PROINFO=&REFERER=");
		// 需要返回的串
		bank.append("MERCHANTID=105220173992017&POSID=545089039&BRANCHID=220000000")
				.append("&ORDERID=" + orderCode + "&PAYMENT=" + money).append("&CURCODE=01&TXCODE=520100")
				.append("&REMARK1=" + callBackUrl + "&REMARK2=" + userId + "&TYPE=1&GATEWAY=&CLIENTIP=&REGINFO=")
				.append("&PROINFO=&REFERER=");
		String mac = new CcbMd5Util().getMD5(ccbStr.toString().trim()).toLowerCase();
		String pub = bank.append("&MAC=" + mac).toString();
		url = "https://ibsbjstar.ccb.com.cn/CCBIS/ccbMain?" + pub;
		return url;
	}
	
	/**
	 * 获取用户的支付笔数
	 */
	@Override
	public Message getUserOrders(String userId) {
		Message message = new Message();
		message.setSuccess(false);
		message.setPayCount(Orders.dao.queryCount(Long.valueOf(userId),DateUtil.formatDate(new Date())));
		return message;
	}
	
	/**
	 * 查询五元话费的集合
	 */
	@Override
	public List<Winning> getPrizeList() {
		List<Winning> list = Winning.dao.getPrizeList();
		return list!=null ? list : new ArrayList<Winning>();
	}
	
	/**
	 * 获取规则集合
	 */
	@Override
	public List<Rule> getRuleList() {
		return Rule.dao.findByRuleAll();
	}
	
	/**
	 * 获取往期回顾
	 */
	@Override
	public List<DateBean> getLuckDrawList(Integer type) {
		List<DateBean> gameReviewList = null;
		// 判断该key是否存在
		if (type != null) {
			switch (type) {
			case 1:
				gameReviewList = DateUtil.getAllTheDateOftheDay();
				break;
			case 2:
				gameReviewList = DateUtil.getAllTheDateOftheWEEK();
				break;
			case 3:
				gameReviewList = DateUtil.getAllTheDateOftheMonth();
				break;
			default:
				break;
			}
		}
		return gameReviewList;
	}
	
	/**
	 *  获取广告列表
	 */
	@Override
	public List<Adver> getAdverList() {
		return Adver.dao.findByAll();
	}
	
	@Override
	public Message getRankingIndex(String userId,String type,String date) {
		Message message  = new Message();
		String index = "";
		String name = "";
		if("1".equals(type)){
			name = "日";
			if(StringUtils.isBlank(date) || "undefined".equals(date)){
				date = new StringBuffer().append(DateUtil.formatDate(new Date()))
						.append("~").append(DateUtil.formatDate(new Date())).toString();
			}
			List<OnList> oList = OnList.dao.findAll(date);
			for (int i = 0;i< oList.size();i++) {
				if(oList.get(i).getUserId().equals(Long.valueOf(userId))){
					index  = new StringBuffer().append("您今日排名为第").append(""+(i+1)+",")
							.append("当前积分：").append(oList.get(i).getScoreSum()).toString();
					break;
				}
			}
		}else if("2".equals(type)){
			name = "周";
			if(StringUtils.isBlank(date) || "undefined".equals(date)){
				date = new StringBuffer().append(DateUtil.fromatStringToDayOfWeek())
						.append("~").append(DateUtil.fromatStringToFirstDayOfWeek()).toString();
			}
			List<WeeklyChart> wList =  WeeklyChart.dao.findAll(date);
			for (int i = 0;i< wList.size();i++) {
				if(wList.get(i).getUserId().equals(Long.valueOf(userId))){
					index  = new StringBuffer().append("您本周排名为第").append(""+(i+1)+",")
							.append("当前积分：").append(wList.get(i).getScoreSum()).toString();
					break;
				}
			}
		}else if("3".equals(type)){
			name = "月";
			if(StringUtils.isBlank(date) || "undefined".equals(date)){
				date = new StringBuffer().append(DateUtil.fromatStringToDayOfMonth())
						.append("~").append(DateUtil.fromatStringToFirstDayOfMonth()).toString();
			}
			List<MonthlyList> mList =  MonthlyList.dao.findAll(date);
			for (int i = 0;i< mList.size();i++) {
				if(mList.get(i).getUserId().equals(Long.valueOf(userId))){
					index  = new StringBuffer().append("您本月排名为第").append(""+(i+1)+",")
							.append("当前积分：").append(mList.get(i).getScoreSum()).toString();
					break;
				}
			}
			
		}else{
			name = "前";
			List<RankingList> rList = RankingList.dao.findAll();
			for (int i = 0;i< rList.size();i++) {
				if(rList.get(i).getUserId().equals(Long.valueOf(userId))){
					index  = new StringBuffer().append("您总榜排名为第").append(""+(i+1)+",")
							.append("当前积分：").append(rList.get(i).getScoreSum()).toString();
					break;
				}
			}
		}
		if(StringUtils.isBlank(index)){
			if("1".equals(type)){
				if(StringUtils.isBlank(date) || "undefined".equals(date)){
					date = new StringBuffer().append(DateUtil.formatDate(new Date()))
							.append("~").append(DateUtil.formatDate(new Date())).toString();
				}
				OnList oList=  OnList.dao.findByUserId(userId,date);
				index = new StringBuffer().append("您当").append(name)
						.append("积分为:").append(oList!=null? oList.getScoreSum(): "0")
						.append(",可惜没有上榜,努力吧少年!").toString();
			}else if("2".equals(type)){
				if(StringUtils.isBlank(date) || "undefined".equals(date)){
					date = new StringBuffer().append(DateUtil.fromatStringToDayOfWeek())
							.append("~").append(DateUtil.fromatStringToFirstDayOfWeek()).toString();
				}
				WeeklyChart oList=  WeeklyChart.dao.findByUserId(userId,date);
				index = new StringBuffer().append("您当").append(name)
						.append("积分为:").append(oList!=null? oList.getScoreSum(): "0")
						.append(",可惜没有上榜,努力吧少年!").toString();
			}else if("3".equals(type)){
				if(StringUtils.isBlank(date) || "undefined".equals(date)){
					date = new StringBuffer().append(DateUtil.fromatStringToDayOfMonth())
							.append("~").append(DateUtil.fromatStringToFirstDayOfMonth()).toString();
				}
				MonthlyList oList=  MonthlyList.dao.findByUserId(userId,date);
				index = new StringBuffer().append("您当").append(name)
						.append("积分为:").append(oList!=null? oList.getScoreSum(): "0")
						.append(",可惜没有上榜,努力吧少年!").toString();
			}else{
				RankingList oList=  RankingList.dao.findByUserId(userId,"2017-07-10"+"~"+"2017-09-30");
				index = new StringBuffer().append("您当").append(name)
						.append("积分为:").append(oList!=null? oList.getScoreSum(): "0")
						.append(",可惜没有上榜,努力吧少年!").toString();
			}
		}
		message.setMessage(index);
		return message;
	}
	
	/**
	 * 是否开启
	 */
	@Override
	public Integer getIntercept(String userId) {
		if (Intercept.dao.queryUserIdExistence(Long.valueOf(userId))) {
			return 1;
		}else {
			return 2;
		}
	}

	@Override
	public String getNameConfig() {
		Cache cache = RedisKit.getCache();
		Map<String,String> map = cache.get(Constants.GAME_CONFIG_PARAMS);
		String date = map.get("help");
		return new Date().getTime() < DateUtil.formatStringToDate(date).getTime() ? "1" : "0";
	}
}
